package com.yi.gostockgo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yi.gostockgo.common.BaseMessage;
import com.yi.gostockgo.controller.vo.GoUserVo;
import com.yi.gostockgo.pojo.GoUser;
import com.yi.gostockgo.mapper.GoUserMapper;
import com.yi.gostockgo.service.GoUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 股民信息 服务实现类
 * </p>
 *
 * @author liandyao
 * @since 2020-04-19
 */
@Service
public class GoUserServiceImpl extends ServiceImpl<GoUserMapper, GoUser> implements GoUserService {

    @Resource
    HttpSession session ;

    /**
     * 用户登录的服务
     * @param user
     * @return BaseMessage
     */
    public BaseMessage login(GoUser user){

        QueryWrapper<GoUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_password",user.getUserPassword());

        queryWrapper.and(wrapper->
                queryWrapper.eq("user_phone",user.getUserPhone()).or().
                        eq("user_account",user.getUserAccount()).or().
                        eq("user_email",user.getUserEmail()));
        GoUser userPo = this.getOne(queryWrapper);
        if(userPo==null){
            return BaseMessage.getBaseFail();
        }else{
            //登录成功之后需要session
            session.setAttribute("goUser",userPo);
            return BaseMessage.getBaseSuccess();
        }
    }

    /**
     * 查询是否存在账号
     * @param user
     * @return
     */
    public BaseMessage checkUser(GoUser user){
        QueryWrapper<GoUser> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("user_phone",user.getUserPhone()).or().
                        eq("user_account",user.getUserAccount()).or().
                        eq("user_email",user.getUserEmail());
        GoUser userPo = this.getOne(queryWrapper);
        if(userPo==null){
            return BaseMessage.getBaseFail();
        }else{
            return BaseMessage.getBaseSuccess();
        }

    }

    @Override
    public Map<String, Object> pagingQuery(GoUserVo goUserVo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tableData", this.getBaseMapper().pagingQuery(goUserVo));
        map.put("count", this.getBaseMapper().pagingQueryCount(goUserVo));
        return map;
    }
}
